#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTimer>
#include "tcpmgr.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 监听注册信号
    connect(ui->loginWid,&LoginWidget::SwitchReg,
            this,&MainWindow::SlotSwitchReg);
    connect(ui->registerWid, &RegisterWidget::SwitchLoin,
               this, &MainWindow::SlotSwitchLogin);
    connect(ui->loginWid,&LoginWidget::SwitchRes,
            this,&MainWindow::SlotSwitchReset);
    connect(ui->resetWidget, &ResetWidget::SwitchLoin,
               this, &MainWindow::SlotSwitchLogin);
    _chatwidget = new ChatWidget();
   connect(TcpMgr::GetInstance().get(),&TcpMgr::SigSwitchChatDLG,this,[=](){
        this->hide();
        this->setGeometry(_chatwidget->geometry());
        QTimer::singleShot(700,this,[=](){
            _chatwidget->show();
        });
    });
   // 处理chat的数据
   ChatConfig();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::ChatConfig()
{

}

void MainWindow::SlotSwitchReg()
{

    qDebug() << "Switching to Register";
    ui->registerWid->Clear();
    SwitchUI(2);
}

void MainWindow::SlotSwitchLogin()
{
    qDebug() << "Switching to Login";
    ui->loginWid->Clear();
    SwitchUI(0);
}

void MainWindow::SlotSwitchReset()
{
    qDebug() << "Switching to reset";
    ui->resetWidget->Clear();
    SwitchUI(1);
}

void MainWindow::SlotSwitchUser()
{

}

void MainWindow::SwitchUI(int page)
{
    // 延迟后切换
    QTimer::singleShot(500,[=](){
        ui->stackedWidget->setCurrentIndex(page);

    });
}



